package com.jerei.store.service.impl;

import com.jerei.store.entity.Order;
import com.jerei.store.entity.OrderItem;
import com.jerei.store.entity.TableCart;
import com.jerei.store.entity.TableCartProduct;
import com.jerei.store.mapper.OrderItemMapper;
import com.jerei.store.mapper.OrderMapper;
import com.jerei.store.mapper.TableCartMapper;
import com.jerei.store.service.IOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderItemServiceImpl implements IOrderItemService {


    @Autowired
    private TableCartMapper tableCartMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void insertOrder(Integer oid, Integer[] arr, String username) {

        OrderItem orderItem=new OrderItem();
        Date date=new Date();
        List<TableCartProduct> list=tableCartMapper.findAllByCid(arr);
        for (TableCartProduct t:list){
            orderItem.setOid(oid);
            orderItem.setPid(t.getPid());
            orderItem.setTitle(t.getTitle());
            orderItem.setImage(t.getImage());
            orderItem.setPrice(t.getPrice());
            orderItem.setNum(t.getNum());
            orderItem.setCreatedUser(username);
            orderItem.setCreatedTime(date);
            orderItem.setModifiedUser(username);
            orderItem.setModifiedTime(date);
            orderItemMapper.insertOrder(orderItem);
        }


    }

    @Override
    public List<Order> findByOid(Integer uid) {


        List<Order> orders=orderMapper.selectByStatus(uid,1);

        return  orders;

    }
}
